﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace BrainIm.UIControls {

	public partial class ClassDiagram : UserControl {

		#region Declares

		private Color m_NodeBackColor = Color.LightGray;

		private Color m_BorderColor = Color.SteelBlue;

		private Color m_BackColor = Color.LightBlue;

		private LinearGradientBrush m_GradientBrush;
		
		#endregion

		#region Constructors

		public ClassDiagram() {
			InitializeComponent();

			pnlHeader.ContextMenuStrip = contextMenuStrip1;
			pnlBody.ContextMenuStrip = contextMenuStrip1;
			//listView1.ContextMenuStrip = contextMenuStrip1;
			txtTitle.ContextMenuStrip = contextMenuStrip1;

			m_GradientBrush = CreateLinearBrush(Color.White, m_BackColor, new Point(0, pnlHeader.Height / 2), new Point(pnlHeader.Width, pnlHeader.Height / 2));
			
		}

		#endregion

		#region Brain Properties
		[Category("Brain Properties"), DefaultValue("Class")]
		public string Title {
			get {
				return this.txtTitle.Text;
			}
			set {
				this.txtTitle.Text = value;
				this.txtTitle.Tag = value;
			}
		}

		[Category("Brain Properties")]
		public int BorderWidth {
			get {
				return this.Padding.All;
			}
			set {
				this.Padding = new Padding(value);
			}
		}
		[Category("Brain Properties")]
		public Color BorderColor {
			get {
				return m_BorderColor;
			}
			set {
				m_BorderColor = value;
				this.Invalidate();
			}
		}
		[Category("Brain Properties")]
		public Color GradientBackColor {
			get {
				return m_BackColor;
			}
			set {
				m_BackColor = value;
				m_GradientBrush = CreateLinearBrush(Color.White, m_BackColor, new Point(0, pnlHeader.Height / 2), new Point(pnlHeader.Width, pnlHeader.Height / 2));
				pnlHeader.Invalidate();
				treeView1.Invalidate();
			}
		}
		[Category("Brain Properties")]
		public Color NodeBackColor {
			get {
				return m_NodeBackColor;
			}
			set {
				m_NodeBackColor = value;
				treeView1.Invalidate();
			}
		}

		#endregion

		#region Control Events

		private void ClassDiagram_Paint(object sender, PaintEventArgs e) {
			e.Graphics.Clear(m_BorderColor);
		}

		#endregion

		#region Title Section

			#region Title Events

			private void txtTitle_DoubleClick(object sender, EventArgs e) {
				SetTitleEditable(true);
			}

			private void txtTitle_Leave(object sender, EventArgs e) {
				SetTitleEditable(false);
			}

			private void txtTitle_KeyDown(object sender, KeyEventArgs e) {
				if (e.KeyCode == Keys.Enter) {
					SetTitleEditable(false);
					txtTitle.Tag = txtTitle.Text;
				}
				if (e.KeyCode == Keys.Escape) {
					SetTitleEditable(false);
					txtTitle.Text = txtTitle.Tag as string;
				}
				this.lblTitle.Text = txtTitle.Text;
			}
			#endregion

			#region Title Methods

			private void SetTitleEditable(bool editable) {
				txtTitle.Visible = editable;
				lblTitle.Visible = !editable;
				if (!editable) lblTitle.Focus();
			}

			#endregion
		

		#endregion

			private void pnlHeader_Paint(object sender, PaintEventArgs e) {
				e.Graphics.FillRectangle(m_GradientBrush, e.ClipRectangle);
			}

			private void lblTitle_DoubleClick(object sender, EventArgs e) {
				SetTitleEditable(true);
			}

			private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) {
				SolidBrush textBrush = new SolidBrush(treeView1.ForeColor);
				if (e.Node.Level == 0) {

					e.Graphics.FillRectangle(m_GradientBrush,e.Node.Bounds.X,e.Node.Bounds.Y,treeView1.Width,e.Node.Bounds.Height);
				} else {
					e.Graphics.FillRectangle(CreateLinearBrush(Color.White, m_NodeBackColor, new Point(0, pnlHeader.Height / 2), new Point(pnlHeader.Width, pnlHeader.Height / 2)), e.Node.Bounds.X, e.Node.Bounds.Y, treeView1.Width, e.Node.Bounds.Height);
				}
				e.Graphics.DrawString(e.Node.Text, treeView1.Font, textBrush, e.Node.Bounds.Location);
			}

			private LinearGradientBrush CreateLinearBrush(Color color1, Color color2, Point from, Point to) {
				//new System.Drawing.Drawing2D.LinearGradientBrush(new Point(0, pnlHeader.Height / 2), new Point(pnlHeader.Width, pnlHeader.Height / 2), Color.White, m_BackColor);
				return new LinearGradientBrush(from, to,color1, color2);
			}

		


	}
}
